
extern "C" {
    pub type _IO_wide_data;
    pub type _IO_codecvt;
    pub type _IO_marker;
    pub type event_base;
    fn readlink(__path: *const i8, __buf: *mut i8, __len: size_t) -> ssize_t;
    fn tcgetpgrp(__fd: i32) -> __pid_t;
    fn fclose(__stream: *mut FILE) -> i32;
    fn fopen(_: *const i8, _: *const i8) -> *mut FILE;
    fn fgetc(__stream: *mut FILE) -> i32;
    fn free(_: *mut ());
    fn setenv(__name: *const i8, __value: *const i8, __replace: i32) -> i32;
    fn unsetenv(__name: *const i8) -> i32;
    fn ioctl(__fd: i32, __request: u64, _: ...) -> i32;
    fn event_init() -> *mut event_base;
    fn xrealloc(_: *mut (), _: size_t) -> *mut ();
    fn xasprintf(_: *mut *mut i8, _: *const i8, _: ...) -> i32;
}
pub type __off_t = i64;
pub type __off64_t = i64;
pub type __pid_t = i32;
pub type __ssize_t = i64;
pub type pid_t = __pid_t;
pub type ssize_t = __ssize_t;
pub type size_t = u64;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct _IO_FILE {
    pub _flags: i32,
    pub _IO_read_ptr: *mut i8,
    pub _IO_read_end: *mut i8,
    pub _IO_read_base: *mut i8,
    pub _IO_write_base: *mut i8,
    pub _IO_write_ptr: *mut i8,
    pub _IO_write_end: *mut i8,
    pub _IO_buf_base: *mut i8,
    pub _IO_buf_end: *mut i8,
    pub _IO_save_base: *mut i8,
    pub _IO_backup_base: *mut i8,
    pub _IO_save_end: *mut i8,
    pub _markers: *mut _IO_marker,
    pub _chain: *mut _IO_FILE,
    pub _fileno: i32,
    pub _flags2: i32,
    pub _old_offset: __off_t,
    pub _cur_column: u16,
    pub _vtable_offset: i8,
    pub _shortbuf: [i8; 1],
    pub _lock: *mut (),
    pub _offset: __off64_t,
    pub _codecvt: *mut _IO_codecvt,
    pub _wide_data: *mut _IO_wide_data,
    pub _freeres_list: *mut _IO_FILE,
    pub _freeres_buf: *mut (),
    pub __pad5: size_t,
    pub _mode: i32,
    pub _unused2: [i8; 20],
}
pub type _IO_lock_t = ();
pub type FILE = _IO_FILE;
#[no_mangle]
pub extern "C" fn osdep_get_name(mut fd: i32, mut _tty: *mut i8) -> *mut i8 {
    let mut f: *mut FILE = std::ptr::null_mut::<FILE>();
    let mut path: *mut i8 = std::ptr::null_mut::<i8>();
    let mut buf: *mut i8 = std::ptr::null_mut::<i8>();
    let mut len: size_t = 0;
    let mut ch: i32 = 0;
    let mut pgrp: pid_t = 0;
    pgrp = unsafe { tcgetpgrp(fd) };
    if pgrp == -(1 as i32) {
        return std::ptr::null_mut::<i8>();
    }
    (unsafe {
        xasprintf(
            &mut path as *mut *mut i8,
            b"/proc/%lld/cmdline\0" as *const u8 as *const i8,
            pgrp as i64,
        )
    });
    f = unsafe { fopen(path, b"r\0" as *const u8 as *const i8) };
    if f.is_null() {
        (unsafe { free(path as *mut ()) });
        return std::ptr::null_mut::<i8>();
    }
    (unsafe { free(path as *mut ()) });
    len = 0 as size_t;
    buf = std::ptr::null_mut::<i8>();
    loop {
        ch = unsafe { fgetc(f) };
        if ch == -(1 as i32) {
            break;
        }
        if ch == '\0' as i32 {
            break;
        }
        buf = (unsafe { xrealloc(buf as *mut (), len.wrapping_add(2 as i32 as u64)) })
            as *mut i8;
        let fresh0 = len;
        len = len.wrapping_add(1);
        (unsafe { *buf.offset(fresh0 as isize) = ch as i8 });
    }
    if !buf.is_null() {
        (unsafe { *buf.offset(len as isize) = '\0' as i32 as i8 });
    }
    (unsafe { fclose(f) });
    return buf;
}
#[no_mangle]
pub extern "C" fn osdep_get_cwd(mut fd: i32) -> *mut i8 {
    static mut target: [i8; 4097] = [0; 4097];
    let mut path: *mut i8 = std::ptr::null_mut::<i8>();
    let mut pgrp: pid_t = 0;
    let mut sid: pid_t = 0;
    let mut n: ssize_t = 0;
    pgrp = unsafe { tcgetpgrp(fd) };
    if pgrp == -(1 as i32) {
        return std::ptr::null_mut::<i8>();
    }
    (unsafe {
        xasprintf(
            &mut path as *mut *mut i8,
            b"/proc/%lld/cwd\0" as *const u8 as *const i8,
            pgrp as i64,
        )
    });
    n = unsafe { readlink(path, target.as_mut_ptr(), 4096 as size_t) };
    (unsafe { free(path as *mut ()) });
    if n == -(1 as i32) as i64
        && (unsafe { ioctl(fd, 0x5429 as i32 as u64, &mut sid as *mut pid_t) }) != -(1 as i32)
    {
        (unsafe {
            xasprintf(
                &mut path as *mut *mut i8,
                b"/proc/%lld/cwd\0" as *const u8 as *const i8,
                sid as i64,
            )
        });
        n = unsafe { readlink(path, target.as_mut_ptr(), 4096 as size_t) };
        (unsafe { free(path as *mut ()) });
    }
    if n > 0 as i64 {
        (unsafe { target[n as usize] = '\0' as i32 as i8 });
        return unsafe { target.as_mut_ptr() };
    }
    return std::ptr::null_mut::<i8>();
}
#[no_mangle]
pub extern "C" fn osdep_event_init() -> *mut event_base {
    let mut base: *mut event_base = std::ptr::null_mut::<event_base>();
    (unsafe {
        setenv(
            b"EVENT_NOEPOLL\0" as *const u8 as *const i8,
            b"1\0" as *const u8 as *const i8, 1,
        )
    });
    base = unsafe { event_init() };
    (unsafe { unsetenv(b"EVENT_NOEPOLL\0" as *const u8 as *const i8) });
    return base;
}
